package com.gagakuai.bpm.dao.mapper.camunda;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.gagakuai.bpm.domian.dto.camunda.CommentDto;
import com.gagakuai.bpm.domian.entity.camuda.ActiInstEntity;
import com.gagakuai.bpm.domian.entity.camuda.HiTaskInstEntity;
import com.gagakuai.bpm.domian.entity.camuda.ProcInstEntity;
import com.gagakuai.bpm.domian.entity.camuda.WorkflowTaskEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface WorkflowTaskMapper extends BaseMapper<WorkflowTaskEntity> {

    /*
     * 查询待办任务
     *
     * @param page         分页组件
     * @param queryWrapper 条件查询
     * @return 待办任务
     */
    @SqlParser(filter = true)
    @Select(" SELECT * from  " +
            " ( " +
            "  SELECT t1.*, t3.NAME_ as PROC_DEF_NAME_ , " +
            "    p.BUSINESS_KEY_, p.START_USER_ID_, u.FIRST_, p.START_TIME_ " +
            "    FROM ACT_RU_TASK t1 " +
            "    INNER JOIN ACT_RE_PROCDEF t2 on t1.PROC_DEF_ID_ = t2.ID_ " +
            "    left JOIN ACT_RE_DEPLOYMENT t3 on t2.DEPLOYMENT_ID_ = t3.ID_ " +
            "    left join ACT_HI_PROCINST p on p.ID_ = t1.PROC_INST_ID_ " +
            "    left join ACT_ID_USER u on u.ID_ = p.START_USER_ID_ " +
            "    ) t " +
            "  ${ew.customSqlSegment}")
    List<WorkflowTaskEntity> listTodoTaskByPage(@Param(Constants.WRAPPER) QueryWrapper<WorkflowTaskEntity> queryWrapper);

    /*
     * 查询已办任务
     *
     * @param queryWrapper 条件查询
     * @return 待办任务
     */
    @SqlParser(filter = true)
    @Select("SELECT * from (" +
            " SELECT t1.*, t3.NAME_ procDefName   FROM ACT_HI_TASKINST t1 " +
            "left JOIN ACT_RE_PROCDEF t2 on t1.PROC_DEF_ID_ = t2.ID_ " +
            "left join ACT_RE_DEPLOYMENT t3 on t3.ID_ = t2.DEPLOYMENT_ID_ "+
            " )t ${ew.customSqlSegment}")
    List<WorkflowTaskEntity> listCompleteTaskByPage(
            @Param(Constants.WRAPPER) QueryWrapper<WorkflowTaskEntity> queryWrapper);

    @Select("select " +
            "       c.TIME_ time, c.USER_ID_ userId, c.TASK_ID_ taskId, c.FULL_MSG_ fullMsg, " +
            "       u.FIRST_ userName, " +
            "       t.NAME_ taskName " +
            " from ACT_HI_COMMENT c " +
            " left join ACT_ID_USER u on c.USER_ID_ = u.ID_ " +
            " left join ACT_HI_TASKINST t on t.ID_ = c.TASK_ID_ " +
            " where c.PROC_INST_ID_ = #{processInstanceId} ")
    List<CommentDto> tableContent(String processInstanceId);


    @Insert(" <script> " +
            " insert into ACT_HI_ACTINST" +
            " (  " +
                " ID_,  PARENT_ACT_INST_ID_,  PROC_DEF_KEY_,  PROC_DEF_ID_,  ROOT_PROC_INST_ID_, " +
                " PROC_INST_ID_,  EXECUTION_ID_,  ACT_ID_,  TASK_ID_,  ACT_NAME_,  ACT_TYPE_, " +
                " ASSIGNEE_,  START_TIME_,  ACT_INST_STATE_ " +
            " )  values  " +
            " <foreach collection = 'tList' item = 't' separator = '),(' open = '(' close = ')' > " +
                "  #{t.id}, #{t.parentActInstId}, #{t.procDefKey}, #{t.procDefId}, #{t.rootProcInstId}, " +
                "  #{t.procInstId}, #{t.executionId}, #{t.actId}, #{t.taskId}, " +
                "  #{t.actName}, #{t.actType}, #{t.assignee}, #{t.startTime}, " +
                "  #{t.actInstState}  " +
            "  </foreach> " +
            " </script> ")
    int intoActInst(List<ActiInstEntity> tList);

    @Insert(" <script> " +
            " insert into ACT_HI_TASKINST" +
            " ( " +
                " ID_,  TASK_DEF_KEY_,  PROC_DEF_KEY_,  PROC_DEF_ID_,  ROOT_PROC_INST_ID_, " +
                " PROC_INST_ID_,  EXECUTION_ID_,  ACT_INST_ID_,  NAME_,  PARENT_TASK_ID_, " +
                " DESCRIPTION_,  OWNER_,  ASSIGNEE_,  START_TIME_,  PRIORITY_ " +
            " ) values " +
            " <foreach collection = 'tList' item = 't' separator = '),(' open = '(' close = ')' > " +
                " #{t.id}, #{t.taskDefKey}, #{t.procDefKey}, #{t.procDefId}, #{t.rootProcInstId}, " +
                " #{t.procInstId}, #{t.executionId},  " +
                " #{t.actInstId}, #{t.name}, #{t.parentTaskId}, #{t.description}, " +
                " #{t.owner}, #{t.assignee}, #{t.startTime}, #{t.priority} " +
            " </foreach> " +
            " </script> ")
    int intoHiTaskInst(List<HiTaskInstEntity> tList);


    @Select(" SELECT ID_ , START_USER_ID_ FROM ACT_HI_PROCINST WHERE ID_ = #{id} ")
    ProcInstEntity queryProcInstEntityById(String id);



    @Delete(" <script> " +
            " delete from ACT_RU_TASK where ID_ in " +
            " <foreach collection = 'taskIdLit' item = 't' separator = ',' open = '(' close = ')' > " +
            " #{t}" +
            " </foreach> " +
            " </script> ")
    int deleteTaskByIdArray(List<String> taskIdLit);

    @Update(" <script> " +
            " update ACT_HI_TASKINST set " +
            " END_TIME_ = #{endTime},  DELETE_REASON_ = 'deleted' " +
            " where ID_ in " +
            " <foreach collection = 'taskIdLit' item = 't' separator = ',' open = '(' close = ')' > " +
            " #{t}" +
            " </foreach> " +
            " </script> ")
    int updateHiTaskInstByIdArray(@Param("taskIdLit")List<String> taskIdLit, @Param("endTime") String endTime);

    @Update(" <script> " +
            " update ACT_HI_ACTINST set END_TIME_ = #{endTime} where ID_ in" +
            " <foreach collection = 'actInstIdList' item = 't' separator = ',' open = '(' close = ')' > " +
            " #{t}" +
            " </foreach> " +
            " </script> ")
    int updateHiActInstById(@Param("actInstIdList")List<String> actInstIdList, @Param("endTime")String endTime);

    @Delete(" <script> " +
            " delete from ACT_RU_EXECUTION where PROC_INST_ID_ = #{procInstId} AND " +
            " ACT_INST_ID_ in " +
            " <foreach collection = 'actInstIdList' item = 't' separator = ',' open = '(' close = ')' > " +
            " #{t}" +
            " </foreach> " +
            " </script> ")
    int deleteExecutionByProcInstIdAndActInstIdArray(@Param("procInstId")String procInstId , @Param("actInstIdList")List<String> actInstIdList);

}
